//
//  MABabyInfoViewController.m
//  meiang_edu
//
//  Created by hfcb on 2018/9/12.
//  Copyright © 2018年 com.meiang.meiang_edu. All rights reserved.
//

#import "MABabyInfoViewController.h"
#import "MAStaffModel.h"
#import "MABabyModel.h"
#import "MAStaffCell.h"
#import "MAStudentEditController.h"

@interface MABabyInfoViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *sexIcon;
@property (weak, nonatomic) IBOutlet UILabel *schoolLabel;
@property (weak, nonatomic) IBOutlet UILabel *classLabel;
@property (weak, nonatomic) IBOutlet UILabel *jigouLabel;
@property (weak, nonatomic) IBOutlet UILabel *nicknameLabel;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;

/** babyModel */
@property(nonatomic,strong) MABabyModel *babyModel;
@property (weak, nonatomic) IBOutlet UIImageView *bgIcon;

/** contactModels */
@property(nonatomic,strong) NSMutableArray *contactModels;

@end

@implementation MABabyInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupBasic];
    
    [self setupTableView];
    
    [self loadData];
    
//    [self loadContactData];
    
    [self wr_setNavBarBackgroundAlpha:0];
}

- (void)setupBasic {
    
}

- (void)bindData {
    
    [self.bgIcon sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMG_OSS,self.babyModel.cbackground]] placeholderImage:[UIImage imageNamed:@""]];
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMG_OSS,self.babyModel.ps_id]] placeholderImage:[UIImage imageNamed:@"homepage_navigationbar_picture"]];
    self.nameLabel.text = self.babyModel.chname;
    self.sexIcon.image = [self.babyModel.aschgender isEqualToString:@"男"] ? [UIImage imageNamed:@"mansymbol_logo"] : [UIImage imageNamed:@"womansymbol_logo"];
    self.schoolLabel.text = self.babyModel.school_name;
    self.classLabel.text = [NSString stringWithFormat:@"%@ %@",self.babyModel.school_grade, self.babyModel.school_class];
    
    self.nicknameLabel.text = self.babyModel.chnickname;
    self.dateLabel.text = self.babyModel.chbirthday;
}

- (void)setupTableView {
//    self.myTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
    self.myTableView.tableFooterView = [UIView new];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)loadData {
    MAUserInfo *userinfo = [MAUserInfoTool userinfo];
    //    if (!userinfo.token) {
    //        [MBManager showBriefAlert:@"登录状态已过期,请重新登录"];
    //        return;
    //    }
    [MBManager showLoading];
    DLog(@"%@",self.title);
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request) {
        request.requestSerializerType = ZBHTTPRequestSerializer;
        request.URLString = [NSString stringWithFormat:@"%@%@",CHILDREN_INFO,self.model.ID];
        request.methodType = ZBMethodTypeGET;
        [request setValue:userinfo.token forHeaderField:@"token"];
        request.apiType = ZBRequestTypeRefresh;
    } success:^(id responseObject, apiType type) {
        [MBManager hideAlert];
        // 解析数据
        NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        DLog(@"%@",dataDict);
        NSInteger code = [dataDict[@"msgcode"] integerValue];
        if (code == 1) { // 请求成功
            [MBManager hideAlert];
            self.babyModel = [MABabyModel mj_objectWithKeyValues:dataDict[@"value"]];
            [self bindData];
            [self loadContactData];
        } else {
            [MBManager showBriefAlert:dataDict[@"message"]];
        }
    } failure:^(NSError *error) {
        DLog(@"%@",error);
        
    } finished:^(id responseObject, apiType type, NSError *error, BOOL isCache) {
        if (isCache==YES) {
            DLog(@"使用了缓存");
        }
        [self.myTableView.mj_header endRefreshing];
    }];
}

- (void)loadContactData {
    MAUserInfo *userinfo = [MAUserInfoTool userinfo];
    //    if (!userinfo.token) {
    //        [MBManager showBriefAlert:@"登录状态已过期,请重新登录"];
    //        return;
    //    }
    [MBManager showLoading];
    DLog(@"%@",self.title);
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request) {
        request.requestSerializerType = ZBHTTPRequestSerializer;
        request.URLString = [NSString stringWithFormat:@"%@%@?entity_type=child_user&status=%@",BASE_LISTENTITY_LIST,self.babyModel.ID,self.babyModel.status];
        request.methodType = ZBMethodTypeGET;
        [request setValue:userinfo.token forHeaderField:@"token"];
        request.apiType = ZBRequestTypeRefresh;
    } success:^(id responseObject, apiType type) {
        [MBManager hideAlert];
        // 解析数据
        NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        DLog(@"%@",dataDict);
        NSInteger code = [dataDict[@"msgcode"] integerValue];
        if (code == 1) { // 请求成功
            [MBManager hideAlert];
            self.contactModels = [MAStaffModel mj_objectArrayWithKeyValuesArray:dataDict[@"value"]];
            [self.myTableView reloadData];
        } else {
            [MBManager showBriefAlert:dataDict[@"message"]];
        }
    } failure:^(NSError *error) {
        DLog(@"%@",error);
        
    } finished:^(id responseObject, apiType type, NSError *error, BOOL isCache) {
        if (isCache==YES) {
            DLog(@"使用了缓存");
        }
        [self.myTableView.mj_header endRefreshing];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self hiddenTabbar:YES];
    if (self.babyModel) {
        [self bindData];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)editBtnDidClick:(UIButton *)sender {
    MAStudentEditController *vc = [[UIStoryboard storyboardWithName:NSStringFromClass([MAStudentEditController class]) bundle:nil] instantiateInitialViewController];
    vc.babyModel = self.babyModel;
    vc.title = @"详情编辑";
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.contactModels.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 64;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    MAStaffModel *model = self.contactModels[row];
    MAStaffCell *cell = [MAStaffCell cellWithTableView:tableView hasCall:YES];
    cell.model = model;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DLog(@"");
    NSInteger row = indexPath.row;
    MAStaffModel *model = self.contactModels[row];
    if (model.others.length) {
        NSString *callMM = model.others;
        NSString *str = [[NSMutableString alloc] initWithFormat:@"tel:%@",callMM];
        if (@available(iOS 10.0, *)) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{} completionHandler:nil];
        } else {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        }
    } else {
        [MBManager showBriefAlert:@"暂无联系方式"];
    }
}

@end
